package com.cyan.app.workflow.fragment.lunbo;

import android.content.Context;
import android.os.Handler;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.cyan.app.workflow.AppData;
import com.cyan.app.workflow.R;
import com.cyan.app.workflow.bean.BannerBean;
import com.cyan.app.workflow.promotion.PromotionTask;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BannerFragment {
	public static final String TAG = "BannerFragment";
	
	private Context mContext;
	private View mBannerView;
	private View mBannerLayout;
	private MyViewPager mViewPager;
	private BannerAdapter mBannerAdapter;
	private ViewPagerFocusView mFocusView;
	private LinearLayout mBannerContainer;
	private boolean mIsRunning = false;
	private PromotionTask mPromotionTaskManager;
	private List<BannerBean.Banner> mImageItems;
	private Map<Integer, Boolean> mIsPageShowMap = new HashMap<Integer, Boolean>();
	private Handler mHandler;
	private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public BannerFragment(Context context, Handler homeHandler) {
		this.mHandler = homeHandler;
		this.mContext = context;
		this.mImageItems = new ArrayList<BannerBean.Banner>();
		BannerBean.Banner  banner = new BannerBean.Banner();
		banner.setImageUrl("http://www.cyan-soft.com/Images/turn2-01.png");
		banner.setImageId(1);
		mImageItems.add(banner);
		BannerBean.Banner  banner1 = new BannerBean.Banner();
		banner1.setImageUrl("http://www.cyan-soft.com/Images/turn4-01.png");
		banner1.setImageId(2);
		mImageItems.add(banner1);
		BannerBean.Banner  banner2 = new BannerBean.Banner();
		banner2.setImageUrl("http://www.cyan-soft.com/Images/turn3-01.png");
		banner2.setImageId(3);
		mImageItems.add(banner2);
	}

	public View createLunBoView(int srcW) {
		mBannerLayout = LayoutInflater.from(mContext).inflate(R.layout.fragment_home_lunbo_container, null);
		mBannerContainer = (LinearLayout) mBannerLayout.findViewById(R.id.container);
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(srcW, srcW * 5 / 12);  // 轮播图片的尺寸宽高比例12:5
		mBannerContainer.setLayoutParams(params);
		return mBannerLayout;
	}
	
	public void initBanner() {
		initBannerView();
		updateBanner();
		updateFocusView();
//		mPromotionTaskManager = new PromotionTask(mContext);
//		mPromotionTaskManager.registerBannerListener(mListener);
//		mPromotionTaskManager.startGetBannerObject();
	}
	
	public void uninitBanner() {
//		mPromotionTaskManager.unregisterBannerListener(mListener);
//		mPromotionTaskManager.stopGetBannerObject();
	}
	
	public boolean checkOnShowTime(long start, long end) {
		Date date = new Date();
		long time = date.getTime()/* / 1000*/;
		return (time >= start) && (time <= end);
	}
	
	public boolean checkVersion(int start, int end) {
	    if (start <= AppData.APPVERSIONCODE && AppData.APPVERSIONCODE <= end) {
	        return true;
	    }
	    
	    return false;
	}

	///获取轮播的图片
	/*private PromotionTask.BannerObjectListener mListener = new PromotionTask.BannerObjectListener() {
		@Override
		public void onUpdate(final BannerBean.Banner banner) {
				if (banner != null) {
					mHandler.post(new Runnable() {
						@Override
						public void run() {
							long start = 0;
							long end = 0;
							try {
								start = format.parse(banner.getStartTime()).getTime();
								end  = format.parse(banner.getEndTime()).getTime();
							} catch (ParseException e) {
								e.printStackTrace();
							}
							if (checkOnShowTime(start, end) &&
							        checkVersion(banner.getMinVersion(), banner.getMaxVersion()) ){
								mImageItems.add(banner);
								initBannerView();
								updateBanner();
							}
						}
					});
				}
		}
	};*/
	
	public void initBannerView() {
		if (mBannerView == null && mImageItems.size() > 0) {
			mBannerView = LayoutInflater.from(mContext).inflate(R.layout.page_store_lunbo, null);
			mViewPager = (MyViewPager) mBannerView.findViewById(R.id.lunbo);
			mFocusView = (ViewPagerFocusView) mBannerView.findViewById(R.id.viewpger_focusview);
			
			mBannerAdapter = new BannerAdapter(mContext, mImageItems);
			mViewPager.setAdapter(mBannerAdapter);
			mViewPager.setOnPageChangeListener(new GuidePageChangeListener());
			
			// 设置初始位置，实现循环滑动。
			int pos = 1000 / mImageItems.size() * mImageItems.size();
			mViewPager.setCurrentItem(pos);
			
			mBannerContainer.removeAllViews();
			mBannerContainer.addView(mBannerView);
		}
		start();
	}
	
	private void updateFocusView() {
		if (mImageItems.size() > 0) {
			mFocusView.setCount(mImageItems.size());
			int currentIndex = mViewPager.getCurrentItem() % mImageItems.size();
			mFocusView.setCurrentIndex(currentIndex);
		}
		
		if (mFocusView != null) {
			if (mImageItems.size() > 1) {
				mFocusView.setVisibility(View.VISIBLE);
			} else {
				mFocusView.setVisibility(View.GONE);
			}
		}
	}
	
	private void updateBanner() {
		if (mImageItems.size() > 0) {
			mBannerAdapter.notifyDataSetChanged();
			updateFocusView();
		}
	}
	
	public void pause() {
		if (mViewPager != null && mIsRunning) {
			mViewPager.stopTimer();
			mIsRunning = false;
		}
	}

	public void start() {
		if (mViewPager != null && !mIsRunning && mImageItems.size() > 1) {
			mViewPager.startTimer();
			mIsRunning = true;
		}
	}
	// 滑动监听
	private class GuidePageChangeListener implements OnPageChangeListener {
		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageSelected(final int pos) {
			int curPos = pos % mImageItems.size();
			mFocusView.setCurrentIndex(curPos);
			reportBannerShow(curPos);
		}
	}
	
	private void reportBannerShow(int curPos) {
		if (mImageItems == null) {
			return ;
		}

		BannerBean.Banner banner = mImageItems.get(curPos);
		int imageId = banner.getImageId();
		Boolean isShown = mIsPageShowMap.get(curPos);
		if (isShown == null || isShown == false) {
			mIsPageShowMap.put(curPos, true);
		}
	}
}
